<?php
namespace Mobile\Controller;
use Think\Controller;
class YuyueController extends Controller{

	/**
	 * [Index 预约页面]
	 */
	public function Index(){
		$datelist = D('Date')->getDateList('','yuyue_day');
		$yuyue_day=array();
		foreach($datelist as $k=>$v){
			$yuyue_day[$v['yuyue_day']] = date('Y年m月d日 ',strtotime($v['yuyue_day']));
		}
		$this->assign('datelist',$yuyue_day);
        $this->assign('title','筛查预约');
        //$this->assign('title','乳腺癌筛查预约');
        $this->display();
	}

	/**
	 * [bespoke 预约处理)
	 * @return [type) [description)
	 */
	public function Bespoke(){
		$data['username'] = I('user');
		$data['phone']	  = I('phone');
		$data['date_day'] = I('date_d');
		$data['date_hour']= I('date_h');
		$data['posttime'] = time();
		$order_model = D('Order');
		$this->checkUser();
		$date_model = D('Date');
		$condition['yuyue_day'] = $data['date_day'];
		$date_info = $date_model->getDateInfo($condition);
		try{
			$order_model->startTrans();
			if(!$result = $order_model->addOrder($data))
				throw new \Think\Exception('预约失败');

			$hour = !empty($date_info['yuyue_time']) ? $date_info['yuyue_time']."|".I('date_h') : I('date_h') ;
			if(!($date_model->saveDate(array('yuyue_time'=>$hour),$condition)))
				throw new \Think\Exception('预约失败');

			$re_data = array('state'=>1,'msg'=>'预约成功');
			$order_model->commit();
		}catch(\Think\Exception $e){
			$re_data = array('state'=>0,'msg'=>'预约失败');
			$order_model->rollback();
		}
		
		$this->ajaxReturn($re_data);
	}

	/**
	 * [checkUser 检查手机号是否重复预约]
	 * @return [type] [description]
	 */
	public function checkUser(){
		$condition['phone'] = I('phone');
		$orderInfo = D('Order')->getOrderInfo($condition);
		if($orderInfo){
			$msg = '您的预约日期：'."\n";
			$msg.= date('Y年m月d日 ',strtotime($orderInfo['date_day']));
			$msg.= date('H时i分',strtotime(str_replace(':','',$this->_getArrTime($orderInfo['date_hour']))));
			$this->ajaxReturn(array('state'=>1,'msg'=>$msg));
		}
	}

	/**
	 * [getTimeData 取得某日期的时间段]
	 * @return [type] [description]
	 */
	public function getTimeData(){
		$date_info = D('Date')->getDateInfo(array('yuyue_day'=>I('date_d')));
		$date = array(
			array('A'=>'10:30','B'=>'10:40','C'=>'10:50'),
			array('D'=>'11:00','E'=>'11:10','F'=>'11:20'),
			array('G'=>'11:30','H'=>'11:40','I'=>'11:50'),
			array('J'=>'12:00','K'=>'12:10','L'=>'12:20'),
			array('M'=>'12:30'),
			array('N'=>'16:10','O'=>'16:20','P'=>'16:30'),
			array('Q'=>'16:40','R'=>'16:50','S'=>'17:00'),
			array('T'=>'17:10')
		);

		$time = explode('|',$date_info['yuyue_time']);
		$this->_getFormatDate($date,$time);
		$this->ajaxReturn($date);
	}

	/**
	 * [cancelYuyue 取消预约]
	 * @return [type] [description]
	 */
	public function cancelYuyue(){
		if(IS_POST){
			$phone = I('phone');
			$order_model = D('Order');
			$date_model = D('Date');
			$order_info = $order_model->getOrderInfo(array('phone'=>$phone));
			$date_info = $date_model->getDateInfo(array('yuyue_day'=>$order_info['date_day']));
			$time = explode('|',$date_info['yuyue_time']);
			foreach($time as $col=>$item){
				if($item == $order_info['date_hour']){
					unset($time[$col]);
				}
			}
			$time_im = implode('|',$time);
			try{
				$order_model->startTrans();
				if(!$order_model->deleteOrder(array('phone'=>$phone)))
					throw new \Think\Exception('取消预约失败');

				if(!$date_model->saveDate(array('yuyue_time'=>$time_im),array('yuyue_day'=>$order_info['date_day'])))
					throw new \Think\Exception('取消预约失败');

				$order_model->commit();
				$re_data = array('state'=>1,'msg'=>'取消预约成功');
			}catch(\Think\Exception $e){
				$re_data = array('state'=>0,'msg'=>'取消预约失败');
				$order_model->rollback();
			}
			$this->ajaxReturn($re_data);
		}
		$this->assign('title','筛查预约');
		$this->display('cancel');
	}

	/**
	 * [_getFormatDate 格式化时间 递归+引用，看不懂我也没办法]
	 * @param  [type] &$date [description]
	 * @param  [type] $arr   [description]
	 * @return [type]        [description]
	 */
	private function _getFormatDate(&$date,$arr){
		foreach($date as $col=> &$item){
			if(is_array($item)){
				$this->_getFormatDate($item,$arr);
			}else{
				$item = in_array($col,$arr) ? $item.'已被预约' : $item ;
			}
		}
	}

	/**
	 * [_getArrTime 替换时间段]
	 * @param  [type] $key [description]
	 * @return [type]      [description]
	 */
	private function _getArrTime($key){
		$date = array(
			'A'=>'10:30','B'=>'10:40','C'=>'10:50',
			'D'=>'11:00','E'=>'11:10','F'=>'11:20',
			'G'=>'11:30','H'=>'11:40','I'=>'11:50',
			'J'=>'12:00','K'=>'12:10','L'=>'12:20',
			'M'=>'12:30',
			'N'=>'16:10','O'=>'16:20','P'=>'16:30',
			'Q'=>'16:40','R'=>'16:50','S'=>'17:00',
			'T'=>'17:10'
		);
		return $date[$key];
	}
}
